package org.hanbo.code.repository.persistence.entity;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.Key;

@PersistenceCapable
public class RegisteredUser {
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;

	@Persistent
	private Email userName;
	
	@Persistent
	private String userDisplayName;
	
	@Persistent
	private Date signedUpDate;
	
	@Persistent
	private boolean active;
	
	@Persistent
	private boolean disabledByAdmin;

	public RegisteredUser()
	{
	}
	
	public Key getUserId()
	{
		return this.key;
	}

	public void setUserName(String userName)
	{
		this.userName = new Email(userName);
	}
	
	public String getUserName()
	{
		if (this.userName != null)
		{
			return this.userName.getEmail();
		}
		
		return null;
	}
	
	public Email getUserEmail()
	{
		return this.userName;
	}
	
	public void setUserDisplayName(String displayName)
	{
		this.userDisplayName = displayName;
	}

	public String getUserDisplayName()
	{
		return this.userDisplayName;
	}
	
	public void createSignedUpDate()
	{
		Calendar dateNow = Calendar.getInstance();
		this.signedUpDate = dateNow.getTime();
	}
	
	public void setSignedUpDate(Date signedupDate)
	{
		this.signedUpDate = signedupDate;		
	}
	
	public Date getSignedUpDate()
	{
		return this.signedUpDate;
	}
	
	public Calendar getSignedUpDateAsCalendar()
	{
		Calendar dateNow = Calendar.getInstance();
		dateNow.setTime(this.signedUpDate);
		
		return dateNow;
	}
	
	public String getSignedUpDateAsString()
	{		
		SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
		String retVal = format.format(this.signedUpDate);
		
		return retVal;
	}
	

	public void setUserActiveStatus(boolean isActive)
	{
		this.active = isActive;
	}
	
	public boolean getUserActiveStatus()
	{
		return this.active;
	}
	
	public void setDisableByAdmin(boolean toBeDisabled)
	{
		if (toBeDisabled)
		{
			this.active = false;
		}
		this.disabledByAdmin = toBeDisabled;
	}
	
	public boolean getDisableByAdmin()
	{
		return this.disabledByAdmin;
	}

	public boolean isUserInfoValid()
	{
		if (userDisplayName == null || userDisplayName.length() <= 0)
		{
			userDisplayName = this.getUserName();
		}
		
		return (userName != null
			&& userDisplayName != null
			&& userDisplayName.length() > 0);
	}
}
